package es.deusto.ingenieria.is.touringproject.formulation;


import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import es.deusto.ingenieria.is.search.formulation.State;
import es.deusto.ingenieria.is.search.xml.StateXMLReader;

public class EnvironmentXMLReader extends StateXMLReader {

	private int cities;
	private City start;
	private City end;
	private static ArrayList <City> list= new ArrayList <City>();

	public EnvironmentXMLReader(String xmlFile) {
		super(xmlFile);
	}

	public State getState() {
		return new Environment(cities,start,end,list);
	}


	//se le llama por cada inicia de etiqueta
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		//qname tiene el nombre de la etiqueta 
		//atributos de la etiqueta

		try {		
			if (qName.equals("is:map")) {
				this.cities = Integer.valueOf(attributes.getValue("cities"));
//				System.out.println(cities);
			} 
			else if (qName.equals("is:start")) {
				start=new City (attributes.getValue("name"),Integer.parseInt(attributes.getValue("x")),Integer.parseInt(attributes.getValue("y")));
			} else if (qName.equals("is:end")) {
				end=new City (attributes.getValue("name"),Integer.parseInt(attributes.getValue("x")),Integer.parseInt(attributes.getValue("y")));
			} else if (qName.equals("is:city")	) {
				City temp = new City ();
				temp.setName(attributes.getValue("name"));
				temp.setCoordx(Integer.parseInt(attributes.getValue("x")));
				temp.setCoordy(Integer.parseInt(attributes.getValue("y")));
				list.add(temp);
				//		this.rightDirt = attributes.getValue("dirt").equalsIgnoreCase("yes");
			}
			
		} catch (Exception ex) {
			System.out.println(this.getClass().getName() + ".startElement(): " + ex);
		}
		
	}
	
	
}